package com.hospital.ecard.cardhandler;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.common.core.module.entity.ecard.BarcodeLogEntity;
import com.hospital.common.core.module.entity.ecard.ResponseEntity;
import com.hospital.common.core.utils.SpringContextHolder;
import com.hospital.ecard.module.entity.PatientInfo;
import com.hospital.ecard.module.service.PatientInfoService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lvyao
 * @description 就诊卡验证
 * @date 2020/7/7
 */
public class VisitCardHandler implements CardHandler {

    private PatientInfoService patientInfoService= SpringContextHolder.getBean(PatientInfoService.class);

    @Override
    public String dealCardInfo(BarcodeLogEntity barcodeLogEntity, HttpServletRequest request) {
        QueryWrapper<PatientInfo> patientInfoEntityWrapper=new QueryWrapper<>();
        ResponseEntity<PatientInfo> cardInfoResEntityResponseEntity=new ResponseEntity<>();
        try {
            String s = barcodeLogEntity.getIdentityNo();
            patientInfoEntityWrapper.eq("CARD_ID",s);
            patientInfoEntityWrapper.eq("card_status","5");
        }catch (StringIndexOutOfBoundsException s){
            cardInfoResEntityResponseEntity.setIsSuccess(10006);
            cardInfoResEntityResponseEntity.setErrMsg("根据传递信息获取患者ID失败！");
            return JSONObject.toJSONString(cardInfoResEntityResponseEntity, SerializerFeature.WriteMapNullValue);
        }
//        if(barcodeLogEntity.isCardFlag()){

//        }
        List<PatientInfo> patientInfoList=patientInfoService.list(patientInfoEntityWrapper);
        if(patientInfoList!=null&&patientInfoList.size()>0){
            cardInfoResEntityResponseEntity.setIsSuccess(10000);
            cardInfoResEntityResponseEntity.setBusinessInfo(patientInfoList.get(0));
        }else {
            cardInfoResEntityResponseEntity.setIsSuccess(10010);
            cardInfoResEntityResponseEntity.setErrMsg("卡未激活或未获取到病人信息");
        }
        return JSONObject.toJSONString(cardInfoResEntityResponseEntity, SerializerFeature.WriteMapNullValue);
    }
}
